﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DPS.Prototype.PolicyBase;
using DPS.Prototype.Factory;
using DPS.Prototype.Provider.Interface;
using DPS.Prototype.Model;
namespace DPS.Prototype.Policy
{
    public class DefaultProdCompetitorAlliance:IProdCompetitorAlliance
    {
        private IProductLineProvider productlineProvider;
        private Dictionary<string, ProdCompetitorBase> prodCompetitors;
        private object lockObj = new object();
        bool isInitialize = false;
        public DefaultProdCompetitorAlliance()
        {
            productlineProvider = Factory.ProductLineProviderFactory.CreateProductLineProvider();
        }

        /// <summary>
        /// Initial all Competitors of Alliance
        /// </summary>
        /// <returns>The count of all competitors</returns>
        private int InitializeAllProdCompetitor()
        {
            if (!isInitialize)
            {
                lock (lockObj)
                {
                    if (!isInitialize)
                    {
                       List<ProdLine> prodLines = productlineProvider.GetAllProductLine();
                       prodCompetitors = new Dictionary<string, ProdCompetitorBase>();
                       prodLines.ForEach(line => {
                           ProdCompetitorBase competitor = ProdCompetitorFactory.CreateProdCompetitor(line);
                           prodCompetitors.Add(competitor.CompetitorName, competitor);
                       });
                       isInitialize = true;
                    }
                }
            }
            return prodCompetitors.Count;
        }

        public Dictionary<string, ProdCompetitorBase> GetAllProdCompetitors()
        {
            InitializeAllProdCompetitor();
            return prodCompetitors;
        }

        public ProdCompetitorBase GetProdCompetitor(string CompetitorName)
        {
            InitializeAllProdCompetitor();
            ProdCompetitorBase competitor = null;
            prodCompetitors.TryGetValue(CompetitorName, out competitor);
            return competitor;
        }

    }
}
